package index

import (
	"html/template"
	"log"
	"myfirstweb/com/zgm/db"
	"net/http"
)

type url struct {
	urlKey   string
	urlValue string
}

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	locals := make(map[string]interface{})
	r.ParseForm()
	//usercode := r.FormValue("usercode")
	t, err := template.ParseFiles("./http/views/index.html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	menus, err := db.Query("select * from menu order by parentmenuid,menuid,sort")
	if err != nil {
		log.Println(err.Error())
	}
	menuTop := make([]interface{}, 0)
	menuLeft := make([]interface{}, 0)
	for _, menu := range menus {
		m := menu.(map[string]interface{})
		if _, ok := m["parentmenuid"]; ok {
			menuLeft = append(menuLeft, m)
		} else {
			menuTop = append(menuTop, m)
		}

	}
	locals["menuTop"] = menuTop
	locals["menuLeft"] = menuLeft
	w.Header().Set("Content-Type", "text/html")
	t.Execute(w, locals)
}
